package com.jf.identity.service.impl;

import com.jf.identity.dao.vis.BehaviorDao;
import com.jf.identity.dao.vis.ExamSessionDao;
import com.jf.identity.dao.visexamid.BehaviorFeedBackDao;
import com.jf.identity.entity.Behavior;
import com.jf.identity.entity.BehaviorFeedBack;
import com.jf.identity.entity.ExamBaseParam;
import com.jf.identity.entity.ExamBaseParamValidator;
import com.jf.identity.exception.ServiceException;
import com.jf.identity.holder.ExamBaseParamHolder;
import com.jf.identity.service.BehaviorService;
import com.jf.identity.util.Params;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.Optional;

/**
 * Created by x on 2018/4/12.
 */
@Service
public class BehaviorServiceImpl implements BehaviorService {
    @Resource
    private ExamBaseParamHolder examBaseParamHolder;
    @Resource
    private ExamSessionDao sessionDao;
    @Resource
    private BehaviorDao behaviorDao;
    @Resource
    private BehaviorFeedBackDao behaviorFeedBackDao;
    @Resource
    private ExamBaseParamValidator examBaseParamValidator;

    @Override
    public Behavior getBehaviorDetails(ExamBaseParam examBaseParam) {
        //先从考试计划拿到参数，然后去查找behaviorid，再通过id去查询相应的数据
//        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、机构识别码、考试计划编号、考试场次编号做验证
        examBaseParamValidator.validate(examBaseParam, ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.ORGCODE);
        String behaviorId = sessionDao.getBehaviorId(examBaseParam);
        Params.isBlank(behaviorId, () -> new ServiceException("当前考试无行为引导库id"));
        //将拿到的id继续查询
        return behaviorDao.getBehaviorDetails(behaviorId);
    }

    @Override
    public boolean insertExaminerBehaviorStatus(BehaviorFeedBack behaviorFeedBack) {
        ExamBaseParam examBaseParam = ExamBaseParam.copyOf(behaviorFeedBack);
        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、考试计划编号、考试场次编号、逻辑考场号做验证
        examBaseParamValidator.validate(examBaseParam, ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.LOGIC_EXAM_ROOM_NUM, ExamBaseParam.ORGCODE);
        //插入数据库需要uuid
        String uid = getExaminerBehaviorUid(behaviorFeedBack, examBaseParam);
        //查询当前行为引导状态。
        Integer examinerStatus = behaviorFeedBackDao.getExaminerStatus(examBaseParam, uid);
        int examRoomStatus = 0;
        //是否更新考场状态
        boolean doUpdateExamRoomStatus = false;
        //初次反馈：
        if (examinerStatus == null) {
            doUpdateExamRoomStatus = true;
        } else if (examinerStatus.equals(Integer.valueOf(behaviorFeedBack.getBehaviorStatus()))) {
            doUpdateExamRoomStatus = false;
        } else if (examinerStatus == 0 && behaviorFeedBack.getBehaviorStatus().equals("1")) {
            doUpdateExamRoomStatus = true;
            //-100
            examRoomStatus = -100;
        } else if (examinerStatus == 1 && behaviorFeedBack.getBehaviorStatus().equals("0")) {
            doUpdateExamRoomStatus = true;
            //-1
            examRoomStatus = -1;
        }
        behaviorFeedBack.setUid(uid);
        int n = behaviorFeedBackDao.insertBehaviorFeedBackInfo(examBaseParam, behaviorFeedBack);
        if (doUpdateExamRoomStatus) {
            insertExamRoomBehaviorStatus(behaviorFeedBack, examRoomStatus);
        }
        return n != 0;
    }

    private String getExaminerBehaviorUid(BehaviorFeedBack behaviorFeedBack, ExamBaseParam examBaseParam) {
        String md5 = examBaseParam.getOrgCode() + examBaseParam.getExamId()
                + examBaseParam.getExamSessionNum() + examBaseParam.getLogicExamRoomNum()
                + behaviorFeedBack.getBehaviorFrom() + behaviorFeedBack.getBehaviorDetailId();
        return DigestUtils.md5DigestAsHex(md5.getBytes(Charset.forName("UTF-8")));
    }


    public boolean insertExamRoomBehaviorStatus(BehaviorFeedBack behaviorFeedBack, int examRoomStatus) {
        ExamBaseParam examBaseParam = ExamBaseParam.copyOf(behaviorFeedBack);
//        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、考试计划编号、考试场次编号、逻辑考场号做验证
//        examBaseParam.validate(ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
//                ExamBaseParam.LOGIC_EXAM_ROOM_NUM, ExamBaseParam.ORGCODE);
        //插入数据库需要uuid
        String uid = getExamRoomBehaviorUid(behaviorFeedBack, examBaseParam);
        Integer status = behaviorFeedBackDao.getExamRoomStatus(examBaseParam, uid);
        status = Optional.ofNullable(status).orElse(0);
        /*
        v_behaviorguide_feedbackinfo每上报一条记录，若v_behaviorguide_feedbackinfo.beh_status=0，
        则v_behaviorguide_feedbackstatus.beh_status+=100；
        若v_behaviorguide_feedbackinfo.beh_status=1，则v_behaviorguide_feedbackstatus.beh_status+=1；
         */
        String eStatus = behaviorFeedBack.getBehaviorStatus();
        if ("0".equals(eStatus)) {
            status += 100 + examRoomStatus;
        } else if ("1".equals(eStatus)) {
            status += 1 + examRoomStatus;
        } else {
            throw new IllegalArgumentException("请输入正确的 行为引导状态 0:正常 1:不正常");
        }
        behaviorFeedBack.setBehaviorStatus(String.valueOf(status));
        behaviorFeedBack.setUid(uid);
        int n = behaviorFeedBackDao.insertBehaviorFeedBackStatus(examBaseParam, behaviorFeedBack);
        return n != 0;
    }

    private String getExamRoomBehaviorUid(BehaviorFeedBack behaviorFeedBack, ExamBaseParam examBaseParam) {
        String md5 = examBaseParam.getOrgCode() + examBaseParam.getExamId()
                + examBaseParam.getExamSessionNum() + examBaseParam.getLogicExamRoomNum()
                + behaviorFeedBack.getBehaviorFrom();
        return DigestUtils.md5DigestAsHex(md5.getBytes(Charset.forName("UTF-8")));
    }
}
